package com.czl.module_storehouse.activity.supplier.home;

import android.app.Dialog;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import androidx.core.content.ContextCompat;
import androidx.core.widget.PopupWindowCompat;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.czl.base.config.AppConstants;
import com.czl.module_base.activity.BaseRecyclerViewActivity;
import com.czl.module_base.bean.CompanyBean;
import com.czl.module_base.bean.ListBean;
import com.czl.module_base.http.HttpResponse;
import com.czl.module_base.mvp.presenter.InjectPresenter;
import com.czl.module_base.mvp.view.SimpleView;
import com.czl.module_service.databinding.LayoutSearchBinding;
import com.czl.module_storehouse.R;
import com.czl.module_storehouse.activity.supplier.add.SupplierAddActivity;
import com.czl.module_storehouse.adapter.SupplierHomeAdapter;
import com.czl.module_storehouse.databinding.CommonButtonBinding;
import com.czl.module_storehouse.event.AddCrmCompanyEvent;
import com.czl.module_storehouse.event.SupplierEvent;
import com.czl.module_storehouse.mvp.presenter.SupplierPresenter;
import com.jeremyliao.liveeventbus.LiveEventBus;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;

/**
 * @author xubin
 */
@Route(path = AppConstants.Router.Service.F_SERVICE_SUPPLIER)
public class SupplierHomeActivity extends BaseRecyclerViewActivity<LayoutSearchBinding> implements SimpleView {

    private SupplierHomeAdapter mAdapter;
    private CompanyBean mSelectCompanyBean;
    @InjectPresenter
    SupplierPresenter mSupplierPresenter;

    @Override
    protected LayoutSearchBinding getChildBinding(LayoutInflater layoutInflater, FrameLayout containerTop) {
        return LayoutSearchBinding.inflate(layoutInflater, containerTop, true);
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        super.initData(savedInstanceState);
        mToolBinding.toolbarContentTitle.setText("供应商");
        CommonButtonBinding buttonBinding = CommonButtonBinding.inflate(getLayoutInflater(), binding.containerBottom, true);
        buttonBinding.button.setText("添加供应商");
        buttonBinding.button.setOnClickListener(v -> goSupplierAddActivity());
        mChildBinding.etSearch.setHint("请输入公司名称搜索");
        EventBus.getDefault().register(this);
        mToolBinding.toolbarRightImg.setBackgroundResource(R.drawable.ic_more_horiz_black_24dp);
        mAdapter = new SupplierHomeAdapter(R.layout.item_supplier_home, new ArrayList<>());
        binding.recyclerView.setAdapter(mAdapter);
        mToolBinding.toolbarRightText.setVisibility(View.VISIBLE);
        mToolBinding.toolbarRightText.setText("编辑");
        initListener();
        LiveEventBus.get(SupplierEvent.class).observe(this, supplierEvent -> onReload());
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void event(AddCrmCompanyEvent event) {

    }

    protected void initListener() {
        mToolBinding.toolbarRightImg.setOnClickListener(v -> showMenu());
        mToolBinding.toolbarRightText.setOnClickListener(v -> {
            mToolBinding.toolbarRightText.setVisibility(View.VISIBLE);
            mAdapter.setEdit(!mAdapter.isEdit());
            if (mAdapter.isEdit()) {
                mToolBinding.toolbarRightText.setText("编辑");
            } else {
                mToolBinding.toolbarRightText.setText("删除");
            }
        });
        mAdapter.setOnItemChildClickListener((adapter, view, position) -> {
            mSelectCompanyBean = mAdapter.getItem(position);
            showDeleteDialog(mSelectCompanyBean.getCompanyId());
        });
        mChildBinding.ivSearch.setOnClickListener(v -> loadData(true, true, mChildBinding.etSearch.getText().toString()));
    }

    private void showDeleteDialog(String companyId) {
        final Dialog dialog = new Dialog(this, R.style.Theme_Light_NoTitle_Dialog);
        View content = View.inflate(getContext(), R.layout.dialog_exit, null);

        TextView txMessage = content.findViewById(R.id.tx_message);
        txMessage.setText("您确定要删除该供应商吗?");

        Button btnCon = content.findViewById(R.id.btn_confirm);
        btnCon.setOnClickListener(v -> {
            dialog.dismiss();
            deleteCrmCompany(companyId);
        });
        Button btnCan = content.findViewById(R.id.btn_cancel);
        btnCan.setOnClickListener(v -> dialog.dismiss());
        dialog.setCancelable(true);
        dialog.setCanceledOnTouchOutside(true);
        dialog.setContentView(content);
        dialog.show();
    }

    private void deleteCrmCompany(String companyId) {
        if (mSupplierPresenter != null) {
            mSupplierPresenter.deleteCrmCompany(companyId);
        }
    }

    private void showMenu() {
        View view = View.inflate(getContext(), R.layout.menu_add_inventory, null);
        TextView textView = view.findViewById(R.id.tv_menu_title);
        textView.setText("删除");
        PopupWindow window = new PopupWindow(getContext());
        window.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
        window.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        window.setContentView(view);
        window.setFocusable(true);
        window.setTouchable(true);
        window.setOutsideTouchable(true);
        window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        PopupWindowCompat.showAsDropDown(window, mToolBinding.toolbarRightImg, 20, 0, Gravity.END);
        view.setOnClickListener(v -> {
            //编辑
            window.dismiss();
            mToolBinding.toolbarRightImg.setVisibility(View.GONE);
            setRightTitle();
            mAdapter.setEdit(!mAdapter.isEdit());
        });
    }

    private void setRightTitle() {
        mToolBinding.toolbarRightText.setText("取消");
        mToolBinding.toolbarRightText.setTextColor(ContextCompat.getColor(getContext(), R.color.color_25A468));
        mToolBinding.toolbarRightText.setTextSize(15);
        mToolBinding.toolbarRightText.setCompoundDrawables(null, null, null, null);
        mToolBinding.toolbarRightText.setVisibility(View.VISIBLE);
    }

    @Override
    protected void loadData() {
        super.loadData();
        loadData(true, false, null);
    }

    @Override
    protected void loadMore() {
        super.loadMore();
        loadData(false, false, null);
    }

    private void loadData(boolean refresh, boolean showDialog, String name) {
        if (mSupplierPresenter != null) {
            mSupplierPresenter.selectCrmCompanyByTag(refresh, showDialog, name);
        }
    }

    @Override
    public <T> void showData(T t) {
        super.showData(t);
        handleResponseData(mAdapter, mSupplierPresenter.getPageNo(), (ListBean<CompanyBean>) t);
    }

    @Override
    public <T> void showHttpResponse(HttpResponse<T> httpResponse) {
        super.showHttpResponse(httpResponse);
        if (SupplierPresenter.TAG_SUPPLIER_DELETE.equals(httpResponse.getRequestTag())) {
            mAdapter.remove(mSelectCompanyBean);
        }
    }

    private void goSupplierAddActivity() {
        startActivity(new Intent(getContext(), SupplierAddActivity.class));
    }
}
